package com.skin.admin.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.skin.admin.entity.Permissions;
import com.skin.admin.entity.Roleperm;
import com.skin.admin.serviceImpl.PermissionsServiceImpl;
import com.skin.admin.serviceImpl.RolepermServiceImpl;
import com.skin.configuration.shiro.ShiroLoad;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/20 0020.
 */
@Component
public class PermManager {

    protected static final Logger log = LoggerFactory.getLogger(PermManager.class);



    @Autowired
    private RolepermServiceImpl rolepermService;

    @Autowired
    private PermissionsServiceImpl permissionsService;

    /**
     * 通过角色ID关联查询到资源
     * @param roleid
     * @return
     */
    public List<Permissions> findPermByRole(String roleid){
        List<Roleperm> rolepermList= rolepermService.selectList(new EntityWrapper<Roleperm>().eq("roleid",roleid));
        List<Permissions> permissionsList=new ArrayList<>();
        for(Roleperm rp:rolepermList){
            permissionsList.add(permissionsService.selectOne(new EntityWrapper<Permissions>().eq("id",rp.getPermid())));
        }
        return permissionsList;
    }

    public Permissions findBy(String colum,String val){
        return permissionsService.selectOne(new EntityWrapper<Permissions>().eq(colum,val));
    }
    public List<Permissions> findListBy(String colum,String val){
        return permissionsService.selectList(new EntityWrapper<Permissions>().eq(colum,val));
    }

    /**
     * 获取所有数据 不分页
     * @return
     */
    public List<Permissions> All(){
        return permissionsService.selectList(new EntityWrapper<Permissions>().eq("flag",1));
    }
    /**
     *
     * 获取所有数据 分页
     * @param current
     * @param size
     * @return
     */
    public Page<Permissions> All(int current , int size){
        return permissionsService.selectPage(new Page<>(current,size),new EntityWrapper<Permissions>());
    }

    @Transactional
    public void deleteById(int id){
        rolepermService.delete(new EntityWrapper<Roleperm>().eq("permid",id));
        permissionsService.deleteById(id);
    }

    /**
     * 更新
     * @param permissions
     */
    public void edit(Permissions permissions){
        if(permissions!=null){
            permissionsService.updateById(permissions);
        }
    }
    public void insert(Permissions permissions){
        if(permissions!=null){
            List<Permissions> size= permissionsService.selectList(new EntityWrapper<Permissions>().eq("url",permissions.getUrl()));
            if(size.size()>0){
                log.info("存在重复数据");
            }else{
                permissionsService.insert(permissions);
            }
        }
    }

    @Transactional
    public int editPermByRoleid(String perm,String roleid){
        String[] permarr =perm.split(",");
        Boolean b=rolepermService.delete(new EntityWrapper<Roleperm>().eq("roleid",roleid));
        if(b) {
            for (int i = 0; i < permarr.length; i++) {
                Roleperm r = new Roleperm();
                r.setRoleid(Integer.parseInt(roleid));
                r.setPermid(Integer.parseInt(permarr[i]));
                rolepermService.insert(r);
            }
            return 1;
        }
        return 0;
    }

}
